# By Henrik Nyh <http://henrik.nyh.se> 2008-01-30.
# Free to modify and redistribute with credit.

# modified by Dave Nolan <http://textgoeshere.org.uk> 2008-02-06
# Ellipsis appended to text of last HTML node
# Ellipsis inserted after final word break

# modified by Mark Dickson <mark@sitesteaders.com> 2008-12-18
# Option to truncate to last full word
# Option to include a 'more' link
# Check for nil last child


require "rubygems"
require "hpricot"

module TextHelper

  # Like the Rails _truncate_ helper but doesn't break HTML tags, entities, and optionally. words.
  def truncate_html(text, options={})    
    return if text.nil?

    max_length = options[:max_length] || 30
    ellipsis = options[:ellipsis] || "..."
    words = options[:words] || false
    # use :link => link_to('more', post_path), or something to that effect

    doc = Hpricot(text.to_s)
    ellipsis_length = Hpricot(ellipsis).inner_text.chars.length
    content_length = doc.inner_text.chars.length
    actual_length = max_length - ellipsis_length

    if content_length > max_length
      truncated_doc = doc.truncate(actual_length)

      if words
        word_length = actual_length - (truncated_doc.inner_html.chars.length - truncated_doc.inner_html.rindex(' '))
        truncated_doc = doc.truncate(word_length)
      end

      last_child = truncated_doc.children.last
      if last_child.inner_html.nil?
        truncated_doc = truncated_doc.inner_html + ellipsis + options[:link] if options[:link]
        truncated_doc.to_s + ellipsis.to_s
      else
        last_child.inner_html = last_child.inner_html.gsub(/\W.[^\s]+$/, "") + ellipsis
        last_child.inner_html += options[:link] if options[:link]
        truncated_doc
      end
    else
      if options[:link]
        last_child = doc.children.last
        if last_child.inner_html.nil?
          doc.inner_html + options[:link]
        else
          last_child.inner_html = last_child.inner_html.gsub(/\W.[^\s]+$/, "") + options[:link]
          doc
        end      
      else
        text.to_s
      end
    end
  end


end

module HpricotTruncator
  module NodeWithChildren
    def truncate(max_length)
      return self if inner_text.chars.length <= max_length
      truncated_node = self.dup
      truncated_node.children = []
      each_child do |node|
        remaining_length = max_length - truncated_node.inner_text.chars.length
        break if remaining_length == 0
        truncated_node.children << node.truncate(remaining_length)
      end
      truncated_node
    end
  end

  module TextNode
    def truncate(max_length)
      # We're using String#scan because Hpricot doesn't distinguish entities.
      Hpricot::Text.new(content.scan(/&#?[^\W_]+;|./).first(max_length).join)
    end
  end

  module IgnoredTag
    def truncate(max_length)
      self
    end
  end
end

Hpricot::Doc.send(:include,       HpricotTruncator::NodeWithChildren)
Hpricot::Elem.send(:include,      HpricotTruncator::NodeWithChildren)
Hpricot::Text.send(:include,      HpricotTruncator::TextNode)
Hpricot::BogusETag.send(:include, HpricotTruncator::IgnoredTag)
Hpricot::Comment.send(:include,   HpricotTruncator::IgnoredTag)
